package com.xuexi.jingdong;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 梁二东
 * @Date: 2024/12/24 13:52
 * @Description: 我们来帮两个老板解决怎么安排会议的问题：
 * 张老板和李老板决定在 10:00~20:00 时间之间安排一个会议，希望会议时长 1 小时，下面是两个人已有的会议安排：
 * 张老板：10:00~11:00, 13:00~14:30, 16:00~19:00
 * 李老板：10:15~12:30, 13:30~14:30, 19:00~20:00
 * 请帮忙找出两个人都空闲可以安排时间段，并打印出来给两个人选择。请用你熟悉的语言来实现。
 **/
@Data
public class JdMeeting {

    public static void main(String[] args) {
        WorkTime workTime = new WorkTime(10, 20);
        List<WorkTime> zhangMeetings = new ArrayList<WorkTime>() {
            {
                add(new WorkTime(10, 11)); // 10:00 ~ 11:00
                add(new WorkTime(13, 14.3)); // 13:00 ~ 14:30
                add(new WorkTime(16, 19)); // 16:00 ~ 19:00
            }
        };
        List<WorkTime> liMeetings = new ArrayList<WorkTime>() {
            {
                add(new WorkTime(10.15, 12.3));
                add(new WorkTime(13.3, 14.3));
                add(new WorkTime(19, 20));
            }
        };
        List<WorkTime> allMeetings = new ArrayList<>(zhangMeetings);
        allMeetings.addAll(liMeetings);
        List<WorkTime> availableSlots = new ArrayList<>();
        allMeetings.sort((a,b) -> Double.compare(a.getStart(), b.getStart()));
        double lastEnd = workTime.start;
        for (WorkTime meeting : allMeetings) {
            if (meeting.start > lastEnd) {
                if (meeting.start - lastEnd >= 1.0) {
                    availableSlots.add(new WorkTime(lastEnd, meeting.start));
                }
                lastEnd = Math.max(lastEnd, meeting.end);
            }
        }
        System.out.println(JSONUtil.toJsonStr(availableSlots));
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class WorkTime {
        private double start;
        private double end;
    }
}


